/**
 * 
 */
package jmine.tec.di.type;

import java.math.BigDecimal;

/**
 * {@link TypeConverter} para {@link BigDecimal}
 * <p>
 * Usa o construtor {@link BigDecimal#BigDecimal(String)}
 * 
 * @author takeshi
 */
public final class BigDecimalTypeConverter implements TypeConverter<BigDecimal> {

    /**
     * {@inheritDoc}
     */
    public BigDecimal convert(String value, Class<?> desiredType) throws TypeConvertionException {
        if ("0".equals(value)) {
            return BigDecimal.ZERO;
        }
        if ("1".equals(value)) {
            return BigDecimal.ONE;
        }
        if ("10".equals(value)) {
            return BigDecimal.TEN;
        }
        // Tratando nossa exportacao que gera numero com virgula quando locale eh pt
        return new BigDecimal(value.replaceAll(",", "."));
    }

    /**
     * {@inheritDoc}
     */
    public boolean isTypeCompatible(Class<?> type) {
        return type == BigDecimal.class;
    }
}